package com.leo.erp.web.account;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.leo.erp.dto.AccountExecution;
import com.leo.erp.entity.AccountVo4InOutList;
import com.leo.erp.enums.AccountStateEnum;
import com.leo.erp.enums.DepotStateEnum;
import com.leo.erp.service.AccountService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/account")
public class AccountManagementController {
	@Autowired
	private AccountService accountService;
	
	/**
	 * 账户流水信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findAccountInOutList")
	@ResponseBody
	public Map<String, Object> findAccountInOutList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();
		//获取前台传过来的值
		Integer currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		Integer pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		int accountId = HttpServletRequestUtil.getInt(request, "accountId");
		Double initialAmount = HttpServletRequestUtil.getDouble(request, "initialAmount");
		
		try {
			List<AccountVo4InOutList> dataList = accountService.findAccountInOutList(accountId, currentPage, pageSize);
			int total = dataList.size();
			map.put("total", total);
			//存放数据json数据
			JSONArray dataArray = new JSONArray();
			if(null != dataList) {
				for(AccountVo4InOutList aEx : dataList) {
					String timeStr = aEx.getOperTime().toString();
					Double balance = accountService.getAccountSum(accountId, timeStr, "date") + accountService.getAccountSumByHead(accountId, timeStr, "date") 
									+ accountService.getAccountSumByDetail(accountId, timeStr, "date") + accountService.getManyAccountSum(accountId, timeStr, "date") + initialAmount;
					aEx.setBalance(balance);
					dataArray.add(aEx);
				}
			}
			map.put("rows", dataArray);
			modelMap.put("data", map);
			modelMap.put("success", true);
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "好像出错了喔");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		String name = HttpServletRequestUtil.getString(request, "name");
		String serialNo = HttpServletRequestUtil.getString(request, "serialNo");
		String remark = HttpServletRequestUtil.getString(request, "remark");
		
		try {
			AccountExecution ae = accountService.getList(name, serialNo, remark, currentPage, pageSize);
			modelMap.put("success", true);
			modelMap.put("pages",ae.getPages());//总页数
			modelMap.put("total", ae.getTotal());//总数
			Map<String, Object> map = new HashMap<>();
			map.put("rows", ae.getAccountPageList());
			map.put("total", ae.getTotal());
			//modelMap.put("userList", ue.getUserPageList());
			modelMap.put("accountList", map);
		} catch (GlobalRuntimeException e) {
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息失败");
		}
		
		
		return modelMap;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		int accountId = HttpServletRequestUtil.getInt(request, "id");
		AccountExecution ae = accountService.updateAccount(beanJson, accountId, request);
		if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/updateAmountIsDefault")
	@ResponseBody
	public Map<String, Object> updateAmountIsDefault(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int isDefault = HttpServletRequestUtil.getInt(request, "isDefault");
		int accountId = HttpServletRequestUtil.getInt(request, "accountId");
		
		AccountExecution ae = accountService.updateAmountIsDefault(isDefault, accountId);
		if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "设置默认出错啦,请找网管");
		}
		
		return modelMap;
	}
	
	/**
	 * 批量删除账户信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/batchDeleteAccountByIds")
	@ResponseBody
	public Map<String, Object> batchDeleteAccountByIds(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String ids = HttpServletRequestUtil.getString(request, "ids");
		String deleteType = HttpServletRequestUtil.getString(request, "deleteType");//1:正常删除,2:强制删除
		AccountExecution ae = new AccountExecution();
		if("1".equals(deleteType)) {
			//正常删除
			ae = accountService.batchDeleteAccountByIdsNormal(ids);
			if(ae.getState() == AccountStateEnum.DELETE.getState()) {
				modelMap.put("success", false);
				modelMap.put("code", 2);
				modelMap.put("msg", "希望强制删除吗?");
			}else if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除帐户信息失败");
			}
		}else if("2".equals(deleteType)) {
			//强制删除
			int effectedNum = accountService.batchDeleteAccountByIds(ids);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除帐户信息失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除操作被拒绝，请联系管理员");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getAccount")
	@ResponseBody
	public Map<String, Object> getAccount(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		AccountExecution ae = accountService.getAccount();
		if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("accountList", ae.getAccountList());
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		//调用方法
		AccountExecution ae = accountService.insertAccount(beanJson, request);
		if(ae.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
				
		return modelMap;
	}
	
	@RequestMapping(value = "/checkIsNameExist")
	@ResponseBody
	public Map<String, Object> checkIsNameExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int accountId = HttpServletRequestUtil.getInt(request, "id");
		String accountName = HttpServletRequestUtil.getString(request, "name");
		//调用方法
		AccountExecution ae = accountService.checkIsNameExist(accountId, accountName);
		if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);//存在
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getStatistics")
	@ResponseBody
	public Map<String, Object> getStatistics(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		AccountExecution ae = accountService.getStatistics();
		if(ae.getState() == AccountStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("data", ae.getAccountMap());
		}else {
			modelMap.put("success", false);
			modelMap.put("data", "获取数据失败");
		}
		
		return modelMap;
	}
}
